package com.namecard.namecardapi.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.namecard.namecardapi.entity.TCate;
import com.namecard.namecardapi.entity.TCommodity;
import com.namecard.namecardapi.entity.vo.SaveTCommodityVo;
import com.namecard.namecardapi.entity.vo.TCommodityVo;
import com.namecard.namecardapi.result.Result;
import com.namecard.namecardapi.service.TCateService;
import com.namecard.namecardapi.service.TCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zm
 * @since 2024-04-21
 */
@DS("slave2")
@Api(tags = "商品管理") //Swagger生成api的提示
@RestController
@RequestMapping("/tCommodity")
public class TCommodityController {
    @Autowired
    private TCommodityService tCommodityService;

    @Resource
    private TCateService tCateService;
    /**
     * 查询全部商品
     */
    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result selectAll(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "commodityVo", value = "查询对象", required = false)
            TCommodityVo commodityVo){
        Page<TCommodity> pageParam = new Page<>(page,limit);
        IPage<TCommodity> tCommodity = tCommodityService.selectAll(pageParam,commodityVo);
        for (TCommodity record : tCommodity.getRecords()) {
            if (record.getCateId()!=null){
                TCate cate = tCateService.getById(record.getCateId());
                if (cate!=null){
                    record.setCateName(cate.getCateName());
                }
            }

        }
        return Result.ok(tCommodity);
    }

    /**
     * 添加商品
     */
    @ApiOperation(value = "添加")
    @PostMapping("/save")
    public Result insert(@RequestBody TCommodity tCommodity){
        tCommodity.setCreateTime(new Date());
        tCommodity.setUpdateTime(new Date());
        tCommodityService.insertCommdity(tCommodity);
        return Result.ok();
    }

    /**
     * 通过id修改商品
     * @param
     * @return
     */
    @ApiOperation(value = "通过id修改商品")
    @PostMapping("/update")
    public Result update(@RequestBody SaveTCommodityVo saveTCommodityVo){
        TCommodity commodity = tCommodityService.getById(saveTCommodityVo.getId());
        if (commodity==null){
            return Result.fail("没有该商品");
        }

        commodity.setCateId(saveTCommodityVo.getCateId());
        commodity.setPageView(saveTCommodityVo.getPageView());
        commodity.setPrice(saveTCommodityVo.getPrice());
        commodity.setRoll(saveTCommodityVo.getRoll());
        commodity.setState(saveTCommodityVo.getState());
        commodity.setTradeName(saveTCommodityVo.getTradeName());
        commodity.setTurnover(saveTCommodityVo.getTurnover());
        commodity.setImgList(saveTCommodityVo.getImgList());
        commodity.setUpdateTime(new Date());
        tCommodityService.updateById(commodity);
        return Result.ok();
    }

    /**
     * 通过id上下架商品
     * * @return
     */
    @ApiOperation(value = "通过id上下架商品")
    @GetMapping("/grounding/{id}")
    public Result updateByState(@PathVariable Long id){
        TCommodity commodity = tCommodityService.getById(id);
        if(commodity==null){
            return Result.fail("没有该商品");
        }
        UpdateWrapper<TCommodity> wrapper = new UpdateWrapper<>();
        if(commodity.getState()==0){
            wrapper.set("state",1).eq("id",id);
            tCommodityService.update(null,wrapper);
        }else{
            wrapper.set("state",0).eq("id",id);
            tCommodityService.update(null,wrapper);
        }
        return Result.ok();
    }


    /**
     * 通过id删除商品
     */
    @ApiOperation(value = "通过id删除商品")
    @GetMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        tCommodityService.delectById(id);

        return Result.ok();
    }


    /**
     * 根据id获取商品信息
     */
    @ApiOperation(value = "通过id获取商品信息")
    @GetMapping("/get/{id}")
    public Result getInfo(@PathVariable Long id){
        TCommodity commodity = tCommodityService.getById(id);
        return Result.ok(commodity);
    }

    /**
     * 通过id是否滚动
     * @return
     */
    @ApiOperation(value = "是否滚动")
    @GetMapping("/roll/{id}")
    public Result roll(@PathVariable Long id){
        TCommodity commodity = tCommodityService.getById(id);
        if(commodity==null){
            return Result.fail("没有该商品");
        }
        UpdateWrapper<TCommodity> wrapper = new UpdateWrapper<>();
        if(commodity.getRoll()==0){
            wrapper.set("roll",1).eq("id",id);
            tCommodityService.update(null,wrapper);
        }else{
            wrapper.set("roll",0).eq("id",id);
            tCommodityService.update(null,wrapper);
        }
        return Result.ok();
    }

    //滚动的商品列表
    @ApiOperation(value = "滚动的商品列表")
    @GetMapping("/rollAll")
    public Result rollAll(){
        QueryWrapper<TCommodity> wrapper = new QueryWrapper<>();
        wrapper.eq("roll",1);
        List<TCommodity> list = tCommodityService.list(wrapper);
        return Result.ok(list);
    }
}

